/**
 * User: Paulo Fournier
 * Date: 13-08-2013
 * Driver app controller
 */

Ext.define('Mobile-app.controller.Driver', {
    extend: 'Ext.app.Controller',

    config: {
        //add view references for this controller
        refs: {
            driverPanel: 'driverpanel',
            driverDetailPanel: 'driverdetailpanel'
        },

        //Definition of all listen events of this controller
        //can be triggered by the [fireEvent] method
        control: {

            driverPanel: {
                showDetails: 'onShowDetails',
                loadStore: 'onLoadStore'
            },

            driverDetailPanel: {
                showMarker: 'onShowMarker'
            }

        }

    },

    /***
     * Used to show the details of a mobile
     * @param list Ext.DataView.List
     * @param record The record associated to the item
     */
    onShowDetails: function(list, record){

        //save record information
        this.selectdRecord = record;

        //load MobileDetail view with this record information
        var view = Ext.create('Mobile-app.view.DriverDetail');

        //add view to body viewport
        Ext.Viewport.add(view);

        //get vehicle details
        Ext.Ajax.request({

            url: 'app/php/public/driver.detail.php',
            method: 'post',
            record: record,
            view: view,
            params: {
                id: record.get('idDriver')
            },

            /***
             * Callback method
             * Called when the AJAX request is performed successfully
             * @param response The response object
             */
            success: function (response, options) {
                //hide the loading mask

                try{

                    //get the JSON object response
                    var rst = Ext.JSON.decode(response.responseText);

                    //check if the authentication was performed successfully
                    if (rst) {

                        if(!options.view.items.first())
                            return;

                        //merge external data with record
                        for(key in rst){
                            options.record.set(key, rst[key]);
                        }

                        //set record to all view panels
                        options.view.items.first().items.each(function(p){p.setRecord(options.record)});

                        //get map object
                        var map = options.view.down('map').getMap();

                        //if map no found, do nothing!
                        if(!map) return;

                        //set position
                        var latLong = new google.maps.LatLng(options.record.get('latitude'), options.record.get('longitude'));

                        //only update map if the record was found
                        if(!map.marker)
                            map.marker = new google.maps.Marker({
                                position: latLong,
                                map: map
                            });
                        else
                            map.marker.setPosition(latLong);

                        //center map
                        map.setCenter(latLong);
                    }
                    else {
                        //otherwise, show the failure auth message
                        console.log('error !success');
                    }

                } catch(e){
                    console.log(e.toString(), e);
                }
            },

            /***
             * Callback method
             * Called when the AJAX request is performed unsuccessfully
             * @param response The response object
             */
            failure: function (response) {

                console.log($.t('error.6', {error: response.statusText}));
            }

        });

    },

    /***
     * Will show the trailer marker on the map and center it
     * @param mapPanel The map object Ext.Map
     */
    onShowMarker: function(mapPanel){

        //set position
        var record = this.selectdRecord,
            map = mapPanel.getMap(),
            latLong = new google.maps.LatLng(record.get('latitude'), record.get('longitude'));

        //check if marker exists, if so, only update the position
        if(!map.marker)
            map.marker = new google.maps.Marker({
                position: latLong,
                map: map
            });
        else
            map.marker.setPosition(latLong);

        //center map
        map.setCenter(latLong);

    },

    /***
     * Will load the list store
     * @param listPanel
     */
    onLoadStore: function(listPanel){
        listPanel.getStore().load();
    }

});